package com.caigou.modules.article.repository

import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.bean.CommentItemBean
import com.caigou.comm.base.bean.HttpResult
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.article.bean.ArticleBean
import com.caigou.modules.article.bean.ArticleItemBean
import com.caigou.modules.article.network.HttpArticleService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/23
 */
@ActivityRetainedScoped
class ArticleRepository @Inject constructor() {

    fun getArticleInfo(userId: String, dataId: Int): Flow<HttpResult<ArticleItemBean>> {
        return HttpArticleService.service.getArticleInfo(userId, dataId)
    }

    fun getMyInfoList(page: Int, limit: Int, userId: String, checkStatus: Int = 0): Flow<HttpResult<ArticleBean>> {
        return HttpArticleService.service.getMyInfoList(page, limit, userId, checkStatus)
    }

    fun setArticleReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
        files.forEach {
            Logger.d(it.toString())
            val fileBody = it.asRequestBody("MultipartFile".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpArticleService.service.setReply(body.build())
    }

    fun setArticleLike(replyId: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dataId", replyId)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpArticleService.service.setArticleLike(body)
    }

    fun setArticleCollect(dataId: Int, userId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("dataId", dataId)
        jsonObject.put("userId", userId)
        val body = jsonObject.toString().toRequestBody()
        return HttpArticleService.service.setArticleCollect(body)
    }

    fun getCommentList(page: Int, limit: Int, userId: String, dataId: Int, sort: Int): Flow<HttpResult<CommentBean>> {
        return HttpArticleService.service.getCommentList(page, limit, userId, dataId, sort)
    }

    fun getCommentDetail(replyId: Int, dataId: Int, userId: String): Flow<HttpResult<CommentItemBean>> {
        return HttpArticleService.service.getCommentDetail(replyId, dataId, userId)
    }

    fun setCommentReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, dataId: Int, parentId: Int): Flow<HttpResult<String>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("dataId", dataId.toString())
            .addFormDataPart("parentId", parentId.toString())
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpArticleService.service.setReply(body.build())
    }

    fun setCommentLike(replyId: Int, senderId: String, receiverId: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("replyId", replyId)
        jsonObject.put("senderId", senderId)
        jsonObject.put("receiverId", receiverId)
        val body = jsonObject.toString().toRequestBody()
        return HttpArticleService.service.setCommentLike(body)
    }
}